package maximumUnits;

import java.util.Arrays;
import java.util.Comparator;

public class Solution {
    public int maximumUnits(int[][] boxTypes, int truckSize) {
        // 思路 贪心 先把单元最多的箱子装上去 再装单元少的
        Arrays.sort(boxTypes, (o1, o2) -> o2[1] - o1[1]);
        int index = 0;
        int ans = 0;
        while (truckSize>0){
            int tmp = truckSize;
            truckSize = Math.max(0,truckSize-boxTypes[index][0]);
            ans+=(tmp - truckSize)*boxTypes[index][1];
            index++;
        }
        return ans;
    }
}
